package mess;

public class LeeCode_63_failed {
    public static void main(String[] args) {
        System.out.println(uniquePathsWithObstacles(new int[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}));
        System.out.println(uniquePathsWithObstacles(new int[][]{{0, 0}, {1, 0}}));
        System.out.println(uniquePathsWithObstacles(new int[][]{{0,0,0,0,0}, {0,0,0,0,1},
                {0,0,0,1,0},{0,0,0,0,0}}));
    }

    private static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1)
            return 0;
        int count = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j == n - 1 && i < m - 1 && obstacleGrid[i + 1][j] == 1)
                    count--;
                if (i < m - 1 && j < n - 1){
                    int right = obstacleGrid[i][j + 1];
                    int down = obstacleGrid[i + 1][j];
                    int self = obstacleGrid[i][j];
                    if (right == 1){
                        break;
                    }
                    if (down != 1 && self != 1) {
                        System.out.println(i + " " + j);
                        count ++;
                    }
                }
                if ((j == 0 || i == m - 1) && obstacleGrid[i][j] == 1){
                    int tempI = i;
                    int tempJ = j;
                    boolean flag = true;
                    while (tempI >= 0 && tempJ <= n - 1){
                        if (obstacleGrid[tempI][tempJ] != 1) {
                            flag = false;
                            break;
                        }
                        tempI--;
                        tempJ++;
                    }
                    if (flag)
                        return 0;
                }
            }
        }
        return count;
    }
}
